import { In_theaters,Subject,Coming_soon,Top250,Top250_loading } from '../../api/top250'

const moive = {
  state: {
    count: null,
    start: null,
    subjects: [],
    total: null,
    id: '',
    subjects_coming: [],
    subject_top250: [],
    subject_refresh:[],
    casts: [],
    images: null,
  },
  mutations: {
    SET_COUNT(state,count){
      state.count = count;
    },
    SET_START(state,start){
      state.start = start
    },
    SET_SUBJECTS(state,subjects){
      state.subjects = subjects
    },
    SET_TOTAL(state,total){
      state.total = total
    },
    SET_ID(state,id){
      state.id = id
    },
    SET_SUBJECTS_COMING(state,subjects_coming){
      state.subjects_coming = subjects_coming
    },
    SET_SUBJECTS_TOP250(state,subject_top250){
      state.subject_top250 = subject_top250
    },
    // 单个电影详情
    SET_CASTS(state,casts){
      state.casts = casts
    },
    SET_IMAGES(state,images){
      state.images = images
    },
    // 上拉刷新
    SET_REFRESH(state,subject_refresh){
      state.subject_refresh = subject_refresh
    }
  },
  actions: {
    //正在热映 
    in_theaters({commit}) {
      return new Promise((resolve, reject) => {
        In_theaters().then(data=>{
          commit('SET_SUBJECTS',data.subjects);
          resolve(data);
        }).catch(error=>{
            reject(error)
        })
      })
    },
    //单个影片详情
    subject_details({commit,state},params){
      return new Promise((resolve, reject) => {
        Subject(params).then(data=>{
          commit('SET_CASTS',data.casts);
          commit('SET_IMAGES',data.images)
          resolve(data);
        }).catch(error=>{
          reject(error)
        })
      })
    },
    // 即将上映
    coming_soon({commit,state}){
      return new Promise((resolve, reject) =>{
        Coming_soon().then(data=>{
          commit('SET_SUBJECTS_COMING',data.subjects);
          resolve(data);
        }).catch(error=>{
          reject(error)
        })
      }) 
    },
    // 排行榜
    top250({commit,state}){
      return new Promise((resolve,reject)=>{
        Top250().then(data=>{
          commit('SET_SUBJECTS_TOP250',data.subjects);
          commit('SET_START',data.start)
          resolve(data);
        }).catch(error=>{
          reject(error)
        })
      })
    },
    // 排行榜刷新
    refresh({commit,stata},loading){
      return new Promise((resolve,reject)=>{
        Top250_loading(loading).then(data=>{
          // commit('SET_REFRESH',data.subjects)
          resolve(data)
        }).catch(error=>{
          reject(error)
        })
      })
    }
  }
}
export default moive